<?php


/**
 * xhr communication class (requ. yui js for comm.)
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @version 1
 * @package core
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * initialize the frontend
 *
 * @package cmsrfe
 * @version 3
 */
class xhranswer
{
	/**
	 * @var object instance
	 */
	private static $INSTANCE;
    /**
     * @var object answer
     */
    private $XHRANSWER;
	/**
	 * instantiate essential objects and make them avail. as subobjects
	 *
	 * @return	void
	 */
	private function __construct()
    {
        // init result
        $this->XHRANSWER = new stdclass();
        $this->XHRANSWER->Results = new stdclass();
        // input field values to update
        $this->XHRANSWER->Results->UpdateValue = array();
        // replace content
        $this->XHRANSWER->Results->replaceContentById = array();
        // messagepopup
        $this->XHRANSWER->Results->MSGHead = '';
        $this->XHRANSWER->Results->MSGFoot = '';
        $this->XHRANSWER->Results->MSGBody = '';
        // js to eval on receive
        $this->XHRANSWER->Results->js = '';
	}
	/**
	 * return a instance of object
	 *
	 * @return object instance
	 */
	public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c;
        }
        return self::$INSTANCE;
    }
    /**
     * return xhr string
     *
     * @return string
     */
    public function __toString()
    {
    	// clean up msg
        if (!$this->XHRANSWER->Results->UpdateValue)
            unset ($this->XHRANSWER->Results->UpdateValue);
        if (!$this->XHRANSWER->Results->replaceContentById)
            unset ($this->XHRANSWER->Results->replaceContentById);
        $json = new Services_JSON();
        return $json->encode($this->XHRANSWER);
    }
    /**
	 * register a value to update, e.g. <input id="ident" value="newval" ....
	 *
     * @param string identifier (id of html object)
     * @param string new value
	 * @return void
	 */
	public function updateValue($identifier = null, $val = '')
    {
    	$this->XHRANSWER->Results->UpdateValue[$identifier] = $val;
    }
    /**
     * set msg popup head
     *
     * @param string head text
     * @return void
     */
    public function setMsgHead ($text = '')
    {
    	$this->XHRANSWER->Results->MSGHead = $text;
    }
    /**
     * set msg popup footer
     *
     * @param string foot text
     * @return void
     */
    public function setMsgFoot ($text = '')
    {
        $this->XHRANSWER->Results->MSGFoot = $text;
    }
    /**
     * set msg popup body
     *
     * @param string body text
     * @return void
     */
    public function setMsgBody ($text = '')
    {
        $this->XHRANSWER->Results->MSGBody = $text;
    }
    /**
     * set msg popup
     *
     * @param string body text
     * @param string head text
     * @param string foot text
     * @return void
     */
    public function setMsg ($body = '', $head = '', $foot = '')
    {
        $this->XHRANSWER->Results->MSGBody = $body;
        $this->XHRANSWER->Results->MSGHead = $head;
        $this->XHRANSWER->Results->MSGFoot = $foot;
    }
    /**
     * evaluate js on receive
     *
     * @param string js code to eval
     * @return void
     */
    public function setJs ($js = '')
    {
        $this->XHRANSWER->Results->js .= $js;
    }
    /**
     * replace content
     *
     * @param string id of container to replace content
     * @param string content to place
     * @return void
     */
    public function replaceContentById ($id = null, $content = null)
    {
        $this->XHRANSWER->Results->replaceContentById[$id] = $content;
    }
}